package com.serkol.ams.controller.api;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.TaocanDetail;
import com.serkol.ams.entity.TaocanMaster;
import com.serkol.ams.service.TaocanDetailService;
import com.serkol.ams.service.TaocanMasterService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/taocan")
public class TaoCanApiController {

    @Autowired
    private TaocanMasterService taocanMasterService;

    @Autowired
    private TaocanDetailService taocanDetailService;


    @IsLogin
    @GetMapping("/findByList")
    public Result findByList(@RequestParam(name = "agentId", defaultValue = "0") long agentId) {
        List<Map<String, Object>> data = taocanMasterService.findByList(agentId);
        return Result.ok(data);
    }

    @IsLogin
    @GetMapping("/findByHomeList")
    public Result findByHomeList() {
         LoginLog loginLog = AuthConstant.getLoginLog();
        List<Map<String, Object>> data = taocanMasterService.findByHomeList(loginLog.getAgentId());
        return Result.ok(data);
    }

    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {

        Result result = new Result();
        return taocanMasterService.findByPageList(pageSize, current);

    }

    @IsLogin
    @GetMapping("/findGoodsList")
    public Result findGoodsList(@RequestParam(name = "taocanId", defaultValue = "0") long taocanId,
                                @RequestParam(name = "agentId", defaultValue = "0") long agentId) {
        List<Map<String, Object>> data = taocanDetailService.findGoodsList(taocanId, agentId);
        return Result.ok(data);
    }


    @IsLogin
    @PostMapping("/edit")
    public Result edit(@RequestBody JSONObject data) {
         LoginLog loginLog = AuthConstant.getLoginLog();
        JSONArray goods = data.getJSONArray("goods");
        List<TaocanDetail> detailList = goods.toJavaList(TaocanDetail.class);
        TaocanMaster master = data.toJavaObject(TaocanMaster.class);
        master.setOperatorId(loginLog.getUserId());
        boolean byId = false;
        if (master.getId() != null && master.getId() > 0) {
            byId = taocanMasterService.updateById(master);
        } else {
            byId = taocanMasterService.save(master);
        }
        if (byId) {
            for (int i = 0; i < detailList.size(); i++) {
                TaocanDetail taocanDetail = detailList.get(i);

                taocanDetail.setTaocanId(master.getId());

                detailList.set(i, taocanDetail);
            }
            boolean b = taocanDetailService.saveOrUpdateBatch(detailList);
            return b ? Result.ok() : Result.failure();
        } else {
            return Result.failure();
        }


    }

    @GetMapping("/findById")
    public Result findById(@RequestParam(value = "id", defaultValue = "0") long id) {

        Map<String, Object> map = taocanMasterService.findById(id);

        return Result.ok(map);
    }

}
